/**   
 * Copyright © 2016 . All rights reserved.
 * 
 * @Title: NettyServer.java 
 * @Project: clusterntss
 * @Package: com.jndk.netty 
 * @Description: 请用一句话描述
 * @author: yuanzhenghe
 * @date: 2016年12月9日 上午10:40:40 
 * @version: V1.0   
 */
package com.jndk.netty;

import java.util.concurrent.ExecutionException;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @ClassName: NettyServer
 * @Description: 请用一句话描述
 * @author: yuanzhenghe
 * @date: 2016年12月9日 上午10:40:40
 */
public class NettyServer {
	
	private static ServerBootstrap bootstrap;

	/**
	 * @Title: main
	 * @Description: 请用一句话描述
	 * @param args
	 * @return: void
	 * @throws Exception 
	 */
	public static void main(String[] args) throws InterruptedException, ExecutionException {

		EventLoopGroup boss = new NioEventLoopGroup(1);
		EventLoopGroup worker = new NioEventLoopGroup();

		bootstrap = new ServerBootstrap();
		bootstrap.group(boss, worker).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
				.childHandler(new ChannelInitializer<SocketChannel>() {
					@Override
					public void initChannel(SocketChannel ch) throws Exception {
						ChannelPipeline p = ch.pipeline();
						p.addLast(new ConsoleHandler());
					}
				});

		// Start the client.
		ChannelFuture ch = bootstrap.bind(8082).sync();

		System.out.println("start server success, you can telnet 127.0.0.1 8082 to send massage for this server");

		// Wait until the connection is closed.
		ch.channel().closeFuture().sync();

	}

}
